package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuEventConfig;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDunHuangShiKuOpenBox;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_OPEN_BOX_FROM_CROSS, desc = "敦煌石窟打开宝箱返回消息到本服")
public class DunHuangShikuOpenBoxFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuOpenBoxToGameServerMsg respMsgToGameServer = DunHuangShiKuProto.DunHuangShiKuOpenBoxToGameServerMsg.parseFrom(packet.getBytes());
        ret = respMsgToGameServer.getRet();
        DunHuangShiKuEventConfig eventConfig = DunHuangShiKuMgr.boxEventMap.get(respMsgToGameServer.getBoxId());
        //恢复预扣除项
        Property property = PropertyHelper.parseStringToProperty(eventConfig.getParam());
        property.rideProperty(respMsgToGameServer.getNum());
        player.getModule(PreReduceModule.class).restorePreReduce(property);

        DunHuangShiKuProto.DunHuangShiKuOpenBoxRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuOpenBoxRespMsg.newBuilder();
        builder.setRet(ret);
        if(ret == 0){
            // 扣道具
            CurrencyModule module = player.getModule(CurrencyModule.class);
            module.removeCurrency(property, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuOpenBoxCost);
            // 获得道具
            Property reward = new Property();
            for (int i = 0; i < respMsgToGameServer.getNum(); i++) {
                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getAllGoods(eventConfig.getTargetId());
                reward.addProperty(openGoodsBagResult.getReward());
            }
            module.addCurrency(reward, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuOpenBoxReward);
            String rewardStr = PropertyHelper.parsePropertyToString(reward);
            // 日志
            AutoLogMgr.add(new LogDunHuangShiKuOpenBox(DunHuangShiKuMgr.activityInfo.getActivityId(), player.getUserId(), respMsgToGameServer.getTeamId(), respMsgToGameServer.getRegionId(), respMsgToGameServer.getBoxId(), respMsgToGameServer.getNum(), rewardStr));
            builder.setReward(rewardStr);
            builder.setBoxId(respMsgToGameServer.getBoxId());
        }
        player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_OPEN_BOX, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
